<?php
namespace app\libs;

use app\exception\HttpExceptions;
use Firebase\JWT\JWT;
use Exception;
use think\facade\Config;

class Token
{
  public static function generateToken($data)
  {
    $token = Config::get('token');
    $nowTime = time();
    $payload = array(
      'iss' => 'http://www.meiji.org',
      'aud' => 'http://www.meiji.com',
      'iat' => $nowTime, //签发时间
      'nbf' => $nowTime, //在什么时间之后该jwt才可用
      'exp' => $nowTime + $token['expire_time'], //过期时间
      'data' => $data
    );
    return JWT::encode($payload, $token['key']);
  }
  public static function parseToken($authorization) {
    $token = Config::get('token');
    try {
        $payload = JWT::decode($authorization, $token['key'], array('HS256'));
        return $payload -> data;
    } catch (Exception $e) {
      throw new HttpExceptions('登录失效，请重新登录', 403);
    }
    
  }
}
